package com.example.zhu95.mytimber.adapters;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.entity.PlayListBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author zhu95
 */
public class PlaylistLinearAdapter extends RecyclerView.Adapter<PlaylistLinearAdapter.ViewHolder> implements SectionIndexer {
    List<PlayListBean> playlists;
    View.OnClickListener itemClickListener;

    public PlaylistLinearAdapter(List<PlayListBean> playlists, View.OnClickListener listener) {
        this.playlists = playlists;
        this.itemClickListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recyclerview_item_plain, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        final PlayListBean playlist = playlists.get(position);
        holder.plainItemTv1.setText(playlist.getTitle());

        List<String> songIDs = playlist.getSongIDs();
        int songCount = 0;
        if (songIDs != null) {
            songCount = songIDs.size();
        }
        holder.plainItemTv2.setText("" + songCount + " 歌曲");
        String albumArt = playlist.getAlbumArt();
        if (!TextUtils.isEmpty(albumArt)) {
            Glide.with(holder.itemView.getContext()).load(albumArt).centerCrop().into(holder.plainItemImg);
        } else {
            Glide.with(holder.itemView.getContext()).load(R.drawable.ic_empty_music2).centerCrop().into(holder.plainItemImg);
        }
        holder.itemView.setTag(playlist.getTitle());
        holder.itemView.setOnClickListener(itemClickListener);
    }

    @Override
    public int getItemCount() {
        return playlists.size();
    }

    @Override
    public Object[] getSections() {
        return playlists.toArray();
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        return sectionIndex;
    }

    @Override
    public int getSectionForPosition(int position) {
        if (position >= playlists.size()) {
            position = playlists.size() - 1;
        }
        return position;
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.plain_item_img)
        ImageView plainItemImg;
        @BindView(R.id.plain_item_tv1)
        TextView plainItemTv1;
        @BindView(R.id.plain_item_tv2)
        TextView plainItemTv2;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
